#include "arrow_item.h"

#include <QPainter>
#include <QPen>
#include <cmath>
#include <QtMath>

ArrowItem::ArrowItem(QPoint start, QPoint end,QGraphicsItem *parent):
    QGraphicsLineItem (parent)
{
    ptEnd = end ; //起点item
    ptStart = start; //终点item
 //   this->setZValue(10); //目的：让箭头后置
    setFlag(QGraphicsItem::ItemIsSelectable);
    setPen(QPen(Qt::black, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
}

QRectF ArrowItem::boundingRect() const
{
    //item绘制区域的估计值
    qreal extra = (this->pen().width() + 20) /2;
    return QRectF(line().p1(),QSizeF(line().p2().x()-line().p1().x(),line().p2().y() - line().p1().y()))
        .normalized().adjusted(-extra,-extra,extra,extra);
}

void ArrowItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{

  //  if(m_pStartItem->collidesWithItem(m_pendItem))//判断图形项是否存在相交
   //     return;

    // this->pen();
      QPen pen=this->pen();//(Qt::black);
      painter->setPen(pen);

     painter->setBrush(pen.color());
        qreal arrowsize = 10;

    QLineF centerLine(ptStart,ptEnd);
   // QPolygonF endPolygon = m_pendItem->polygon();//获取item的点组合（闭环路）
    QPointF p1 = ptStart;//endPolygon.first() + ptEnd;//item的左上方点  + item在场景的位置点（偏移得到左上点在场景中的位置）
    QPointF p2 =   ptEnd;
  //  QPointF intersectPoint;
   // QLineF polyLine;
    // for(int i = 1;i < endPolygon.count(); ++i)
    // {
    //     p2 = endPolygon.at(i) +ptEnd;//偏移得到场景中的位置点
    //     polyLine = QLineF(p1,p2);//两点构成直线

    //     QLineF::IntersectType intersectType =
    //         polyLine.intersect(centerLine,&intersectPoint);//两条线比较是否有交集   存在
    //     if(intersectType == QLineF::BoundedIntersection)
    //         break;
    //     p1 = p2;
    // }

 //   painter->setPen(Qt::black);
    setLine(QLineF(ptEnd,ptStart));

    double angle = std::atan2(-line().dy(),line().dx()); //反正切 [-PI,PI]
    QPointF arrowP1 = line().p1() +
                      QPointF(sin(angle + M_PI / 3) * arrowsize, //计算对边
                              cos(angle + M_PI / 3) * arrowsize); //计算临边
    QPointF arrowP2 = line().p1() +
                      QPointF(sin(angle + M_PI - M_PI / 3) * arrowsize,
                              cos(angle + M_PI - M_PI / 3) * arrowsize);

    arrowHead.clear();
    arrowHead<<line().p1() << arrowP1 << arrowP2;

    painter->setPen(pen);
    painter->drawLine(line());
    painter->drawPolygon(arrowHead);

}

QPainterPath ArrowItem::shape() const
{
    QPainterPath path = QGraphicsLineItem::shape();
    path.addPolygon(arrowHead);
    return path;
}
